package com.tm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tm.dto.DishDTO;
import com.tm.dto.DishDTOD;
import com.tm.entity.DishEntity;

import com.tm.entity.DishFlavorEntity;
import com.tm.mapper.DishFlavorMapper;
import com.tm.mapper.DishMapper;
import com.tm.service.DishFlavorService;
import com.tm.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author huangp
 * @create 2023-04-05 10:35
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, DishEntity> implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    public Page<DishEntity> pageList(Integer page, Integer pageSize, DishDTO dishDTO) {

        Page<DishEntity> page1 = new Page<>(page,pageSize);
        QueryWrapper<DishEntity> queryWrapper = new QueryWrapper<DishEntity>();
        queryWrapper.eq("d.is_deleted",0);
        queryWrapper.eq("c.type",1);
        queryWrapper.like(StringUtils.isNotBlank(dishDTO.getName()),"d.name",dishDTO.getName());
        Page<DishEntity> page2 = dishMapper.pageList(page1,queryWrapper);
        return page2;

    }

   /* @Override
    public Boolean saveWithFlavor(DishEntity dishEntity) {
        return dishMapper.insert(dishEntity) > 0;
    }*/

    @Override
    @Transactional
    public void saveWithFlavor(DishDTOD dishDTOD) {
        //保存菜品信息到数据库
        this.save(dishDTOD);
        Long dishid = dishDTOD.getId();
        //菜品口味
        List<DishFlavorEntity> flavors = dishDTOD.getFlavors();
        flavors.stream().map((x) -> {
            x.setDishId(dishid);
            return x;
        }).collect(Collectors.toList());
        //保存口味数据表到菜品口味表
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public Boolean updateIds(List<Long> ids) {
        return dishMapper.selectIds(ids) > 0;
    }

    @Override
    public DishDTOD getByIfwithFlavor(Long id) {
        //查询菜品基本信息
        DishEntity dishEntity = this.getById(id);
        DishDTOD dishDTOD = new DishDTOD();
        //给类相同字段赋值
        BeanUtils.copyProperties(dishEntity,dishDTOD);
        LambdaQueryWrapper<DishFlavorEntity> queryWrapper = new LambdaQueryWrapper<>();
        //遍历取相同id的信息
        queryWrapper.eq(DishFlavorEntity::getDishId,dishEntity.getId());
        List<DishFlavorEntity> list = dishFlavorService.list(queryWrapper);
        dishDTOD.setFlavors(list);
        return dishDTOD;
    }


    /**
     * 修改菜品状态
     * @param status
     * @param ids
     */
    @Override
    public void dishStatusByStatus(Integer status,List<Long> ids) {
        UpdateWrapper<DishEntity> wrapper = new UpdateWrapper<>();
        wrapper.in("id",ids);
        DishEntity dishEntity = new DishEntity();
        dishEntity.setStatus(status);
        super.update(dishEntity,wrapper);
    }

    @Override
    public void updateWithFlavor(DishDTOD dishDTOD) {

        //更新dish表基本信息
        this.updateById(dishDTOD);
        //清理当前菜品对应口味数据--dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavorEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavorEntity::getId, dishDTOD.getId());
        dishFlavorService.remove(queryWrapper);
        //添加当前提交过来的口味数据--dish_flavro表的insert操作
        List<DishFlavorEntity> flavors = dishDTOD.getFlavors();
        flavors = flavors.stream().map((x) -> {
            x.setDishId(dishDTOD.getId());
            return x;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);

    }

}
